import urllib,sys
'''from HTMLParser import HTMLParser'''

class AdvHTMLParser:
	def __init__(self):
		self.inScript	= False;
		
	def feed(self, text):
		lenHtml	= len(text)
		start	= 0
		while start !=-1:
			startTag	= text.find('<',start)
			
			if startTag == -1:
				self.handle_data( text[start:] )
				return
			
				
			self.handle_data( text[start:startTag] )
			start	= self.ProcessTag( text, startTag )			
			
			if self.inScript == True:
				start = self.processScriptToEnd( text, start )
					
	def ProcessTag( self, text, start ):
		end	= text.find('>',start)
		if end == -1:
			return start+1
		
			
		tagseg	= text[start+1:end]
		sep		= tagseg.find(' ')
		
		if sep == -1:
			tag		= tagseg
			attrseg	= None
		else:
			tag		= tagseg[0:sep]
			attrseg	= tagseg[sep+1:]

		if tag[0] == '/':
			self.handle_endtag( tag[1:] )
		else:
			self.internalStartTag( tag, attrseg )
			
		return end+1
	
	def ToAttrs(self, attrseg):			
		if len(attrseg) ==0:
			return attrseg
		
		attrseg	= attrseg.replace('\t', ' ')
		attrseg	= attrseg.replace('\n', ' ')
		attrseg	= attrseg.replace('\r', ' ')
		attrseg	= attrseg.replace('\'', '"')
		attrseg	= attrseg.lstrip()
		attrseg	= attrseg.rstrip()
		
		while attrseg.find('  ') != -1:
			attrseg	= attrseg.replace('  ', ' ')
			
		result	= list()
		args	= self.splitArgs(attrseg)
		
		for arg in args:
			ndx		= arg.find('=')
			if ndx == -1:
				name	= arg
				value	= ""
			else:
				name	= arg[0:ndx]
				value	= arg[ndx+1:].replace( '"', '')
			
			result.append( (name,value) )
			
		return result

	def splitArgs(self,attrseg):
		args	= []
		seglen	= len(attrseg)
		start	= 0
		while start !=-1:
			if start >= seglen:
				break
							
			while( attrseg[start] == ' ' ):
				start = start+1
				
			spacendx	= attrseg.find(' ',start)
			valtag		= attrseg.find('=',start)
			if (valtag == -1) | (spacendx<valtag):
				if spacendx == -1:
					args.append( attrseg[start:] )
					start	= -1
				else:
					args.append( attrseg[start:spacendx] )
					start = spacendx+1
				continue
			
			valtag = valtag+1
				
			while( attrseg[valtag] == ' ' ):
				valtag	= valtag+1
				
			quottype	= attrseg[valtag]
			endquot		= attrseg.find(quottype,valtag+2)
			
			if endquot == -1:
				args.append( attrseg[start:] )
				start	= -1
			else:
				args.append( attrseg[start:endquot+1] )
				start	= endquot+2
			continue

		return args
	
	def internalStartTag(self, tag, attrseg):
		if tag == None:
			return
			
		if tag.lower() == "script":
			self.inScript	= True;
		
		if tag[0] == '!':
			if attrseg == None:
				attrseg = ''
			else:
				attrseg = ' '+attrseg
				
			if tag.find('!--') == 0:
				self.handle_comment(tag[3:len(tag)-2])
				return

			self.handle_decl(tag[1:]+attrseg)
			return

		if tag[0] == '?':
			if attrseg == None:
				attrseg = ''
			else:
				attrseg = ' '+attrseg
			self.handle_pi(tag[1:]+attrseg)
			return
			
		if attrseg == None:
			self.handle_starttag(tag, None)
			return
		
		if len(attrseg)==0:
			self.handle_starttag(tag, None)
			return

		if attrseg[-1] == '/':
			attrs	= self.ToAttrs(attrseg[0:len(attrseg)-1])
			self.handle_starttag(tag, attrs)
			self.handle_endtag(tag)
		else:
			attrs	= self.ToAttrs(attrseg)
			self.handle_starttag(tag, attrs)
		return

	def processScriptToEnd( self, text, start ):
		ndxEnd	= self.findEndScript( text, start )
		if ndxEnd == -1:
			return start
			
		self.handle_data( text[start:ndxEnd] )
		self.inScript	= False
		return ndxEnd+1
		
				
	def findEndScript( self, text, start ):
		next	= start
		while next !=-1:
			startTag	= text.find('<',next)
			if startTag == -1:
				return start
				
			endTag		= text.find('>',startTag)
			
			if  endTag-startTag == 8:
				if text[startTag:endTag+1].lower() == "</script>":
					return startTag
			
			next		= endTag+1
		return -1
		
	def handle_decl(self, tag):
		return
		
	def handle_pi(self, tag):
		return
		
	def handle_comment(self, tag):
		return
		
	def handle_data(self, data):
		return
				
	def handle_endtag(self, tag):
		return
		
	def handle_starttag(self, tag, attrs):
		return


'''if __name__ == '__main__':
	AdvHTMLParser().feed(urllib2.urlopen("http://www.tnrcn.com/").read())
'''	
	
